/**
 * $Id$
 *
 * Unpublished Work Copyright 2013 Confirmit, Inc. All Rights Reserved.
 *
 * Last changed by $Author$
 * Last changed at $Date$
 */
package com.youtills.core.app.calc;

import com.youtills.core.app.common.ISiteLayout;
import com.youtills.core.app.common.SiteLayout;
import com.youtills.core.calc.Loan;
import com.youtills.core.lang.StringUtil;
import com.youtills.core.web.IRequestHandler;

/**
 * TODO
 *
 * @author Ravi Kumar Chiluveru (ravic)
 * @since 12.2, (5/18/13 3:33 PM)
 */
public class LoanCalcRequestHandler extends SiteLayout implements IRequestHandler, ISiteLayout {

    private Loan loan;

    public String getMainContentPage() {
        return "/WEB-INF/jsp/calc/loan.jsp";
    }

    public String getPrincipal(){
        return getContextBean().getRequest().getParameter("principal");
    }

    public Loan getLoan() {

        if(loan == null){
            String action = getContextBean().getRequest().getParameter("action");
            if("Calculate".equals(action)){
                double principal = StringUtil.getNumber(getPrincipal(), 0).doubleValue();
                double rate = StringUtil.getNumber(getRate(), 0).doubleValue();
                int term = StringUtil.getNumber(getTerm(), 0).intValue();
                loan = new Loan(principal, rate, term);
                loan.calculatePayment();
            }
        }

        return loan;
    }

    public String getTerm(){
        return getContextBean().getRequest().getParameter("term");
    }

    public String getRate() {
        return getContextBean().getRequest().getParameter("rate");
    }
}
